package org.tobuy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tobuy.vo.*;
import org.tobuy.factory.ItemDAOFactory;
import org.tobuy.factory.UserDAOFactory;
import org.tobuy.factory.ScoreDAOFactory;

public class ShowItemInfo extends HttpServlet {

	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		User seller = new User();
		String path = "index.jsp?action=showItemInfo";
		String item_id = (String) req.getParameter("itemid");
		Item item = new Item();
		try {
			item = ItemDAOFactory.getIItemDAOInstance().findItem(item_id);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if("book".equals(item.getType())){
			Book book = new Book();
			try {
				book = ItemDAOFactory.getIItemDAOInstance().findBook(item_id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("item", book);
		}		
		if("dvd".equals(item.getType())){
			Dvd dvd = new Dvd();
			try {
				dvd = ItemDAOFactory.getIItemDAOInstance().findDvd(item_id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("item", dvd);
		}
		if("game".equals(item.getType())){
			Game game = new Game();
			try {
				game = ItemDAOFactory.getIItemDAOInstance().findGame(item_id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("item", game);
		}
		
		List<Score> scores = new ArrayList<Score>();
		try {
			scores = ScoreDAOFactory.getIScoreDAOInstance().findItemScoreSet(item);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			seller = UserDAOFactory.getIUserDAOInstance().findUser(item.getSeller_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			seller= UserDAOFactory.getIUserDAOInstance().findUser(item.getSeller_id());
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.setAttribute("scores", scores);
		req.getRequestDispatcher(path).forward(req, resp);
		System.out.println(item.getSeller_id());

		
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		this.doGet(req,resp);
	}

}
